const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');


  let topics = await db.collection('topics')
    .find(
      {tab: 'podcast', title: / 🎧$/}
    ).sort({create_at: -1})
    .limit(5000)
    .toArray();


  for (let i = 0; i < topics.length; i++) {
    let topic = topics[i];
    if (topic.title.endsWith('🎧')) {
      topic.title = topic.title.replace(' 🎧', '');
      topic.title = '🎧 ' + topic.title;
    } else {
      console.error(topic.title);
    }

    console.log(topics.length, i, topic.title);
    let result;
    try {
      result = await db.collection('topics').updateOne({_id: topic._id}, {
        $set: {
          title: topic.title,
        },
      });
    } catch (e) {
    }
  }

  client.close();

}

main();
